package com.apes.fn.scm.merchant.account.model;

import com.alibaba.fastjson.annotation.JSONField;
import com.apes.fn.fico.expense.ExpenseItem;
import com.apes.fn.scm.merchant.settlement.model.MerchantSettlement;
import com.apes.framework.jpa.BaseModel;
import com.apes.framework.jpa.DefaultPersistListener;
import com.apes.framework.jpa.comment.Comment;
import com.apes.framework.jpa.generator.ApesGenerator;
import com.apes.framework.util.ApesConst;
import com.apes.framework.util.DateUtil;
import com.apes.scm.income.model.IncomeItem;
import com.apes.scm.masterdata.finance.model.IncomeType;
import com.apes.scm.masterdata.party.model.PartyRole;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.SelectBeforeUpdate;

import javax.persistence.*;
import javax.validation.constraints.NotNull;
import java.util.Date;
import java.util.Objects;

/**
 * @BelongsProject: apes-server
 * @BelongsPackage: com.apes.fn.scm.merchant.account.model
 * @ClassName: SettlementExpenseItem
 * @Author: pyb
 * @Description: 赋能商户结算费用明细
 * @CreateTime: 2021-09-29 16:21
 * @Version: 1.0
 */
@Getter
@Setter
@SelectBeforeUpdate
@DynamicUpdate
@DynamicInsert
@Entity
@EntityListeners(DefaultPersistListener.class)
@Table(name = SettlementExpenseItem.CONST_TABLE_NAME)
public class SettlementExpenseItem extends BaseModel {
    public static final String CONST_TABLE_NAME = "merchant_expense_item";
    @Id
    @Column(name = "id", length = 32)
    @ApesGenerator(code = CONST_TABLE_NAME, name = "赋能费用明细", padding = 8)
    @Comment("编码")
    private String id;


    @NotNull
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "income_item_id", referencedColumnName = "id")
    @Comment("费用单项目")
    private IncomeItem incomeItem;


    @ManyToOne(optional = false)
    @JoinColumn(name = "merchant_settlement_id", referencedColumnName = "id", nullable = false)
    private MerchantAccountSettlement merchantSettlement;


    @Column(name = "current_amount", nullable = false, columnDefinition = ApesConst.FIELD_MONEY_16_4_0)
    @Comment("本期扣收金额")
    private double currentAmount = 0;


    @Column(name = "amount_deducted", nullable = false, columnDefinition = ApesConst.FIELD_16_4_0)
    @Comment("往期已扣收金额")
    private double amountDeducted = 0;


    @Column(name = "amount", nullable = false, columnDefinition = ApesConst.FIELD_16_4_0)
    @Comment("费用金额")
    private double amount = 0;

    @Column(name = "balance_amount", nullable = false, columnDefinition = ApesConst.FIELD_16_4_0)
    @Comment("结余金额")
    private double balanceAmount = 0;

    @Transient
    private String partnerName;

    @Transient
    public String getPartnerName() {
        if (incomeItem == null) return null;
        if(incomeItem.getIncome() == null) return null;
        return incomeItem.getIncome().getPartner().getId();

    }

    @Transient
    private String annex;

    @Transient
    public String getAnnex() {
        if (incomeItem == null) return null;
        return incomeItem.getAnnex();

    }

    @Transient
    private IncomeType incomeType;

    @Transient
    public IncomeType getIncomeType() {
        if (incomeItem == null) return null;
        return incomeItem.getIncomeType();

    }

    @Transient
    private String expenseId;

    @Transient
    public String getExpenseId() {
        if (incomeItem == null) return null;
        if(incomeItem.getIncome() == null) return null;
        return incomeItem.getIncome().getId();

    }

    @Transient
    private String incomeItemId;

    @Transient
    public String getIncomeItemId() {
        if (incomeItem == null) return null;
        return incomeItem.getId();

    }


    @JSONField(format = DateUtil.SHOW_DATE_FORMAT)
    @Transient
    private Date periodStart;

    @Transient
    public Date getPeriodStart() {
        if (incomeItem == null) return null;
        return incomeItem.getStartDate();

    }

    @Transient
    private String note;

    @Transient
    public String getNote() {
        if (incomeItem == null) return null;
        return incomeItem.getNote();

    }

    @JSONField(format = DateUtil.SHOW_DATE_FORMAT)
    @Transient
    private Date periodEnd;

    @Transient
    public Date getPeriodEnd() {
        if (incomeItem == null) return null;
        return incomeItem.getEndDate();

    }
    @JSONField(format = DateUtil.SHOW_DATE_FORMAT)
    @Transient
    private Date approveDate;

    @Transient
    public Date getApproveDate() {
        if (incomeItem == null) return null;
        if(incomeItem.getIncome() == null) return null;
        return incomeItem.getIncome().getApproveDate();

    }


    @Transient
    private String source;

    @Transient
    public String getSource() {
        if (incomeItem == null) return null;
        if(incomeItem.getIncome() == null) return null;
        return incomeItem.getIncome().getSource();

    }

    @Transient
    private String sourceBusinessType;

    @Transient
    public String getSourceBusinessType() {
        if (incomeItem == null) return null;
        if(incomeItem.getIncome() == null) return null;
        return incomeItem.getIncome().getSourceBusinessType();

    }

}
